import { configureStore, createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import $ from '../axios'
// 创建异步action来获取数据
const fetchList = createAsyncThunk(
  'fetchList',
  async (str, { rejectWithValue }) => {
    try {
      const response = await $.post('/gethomegood', str);
      return response.data;
    } catch (error) {
      return rejectWithValue(error);
    }
  }
);

const fetchComments = createAsyncThunk(
  'fetchComments',
  async (_, { rejectWithValue }) => {
    try {
      const response = await $.get('/getComment');
      return response.data;
    } catch (error) {
      return rejectWithValue(error);
    }
  }
);

const fetchTopic = createAsyncThunk(
  'fetchTopic',
  async (_, { rejectWithValue }) => {
    try {
      const response = await $.get('/getTopic');
      return response.data;
    } catch (error) {
      return rejectWithValue(error);
    }
  }
);

const stateSlice = createSlice({
  name: 'state',
  initialState: {
    detail: [],
    list: [],
    actKey: "",
    chat: sessionStorage.getItem('heData') ? JSON.parse(sessionStorage.getItem('heData')) : {},
    Myid: sessionStorage.getItem('Myid') ? sessionStorage.getItem('Myid') : '',
    comments: [],
    topic: [],
    guang: [],
    lastExpressions: ['&#128512;'],
    searchList: [],
  },
  reducers: {
    goDetail: (state, action) => {
      state.detail = [action.payload];
    },
    setguang(state, { payload }) {
      state.guang.push(payload)
    },
    getguang(state, { payload }) {
      console.log(111);
    },
    setAct(state, action) {
      state.actKey = action.payload; // 修改订单列的显示
    },
    setChat(state, action) {
      state.chat = action.payload // 修改和谁聊天
    },
    setMyId(state, action) {
      state.Myid = action.payload
    },
    setLastExpressions(state, action) {
      if (state.lastExpressions.length === 7) return
      state.lastExpressions.push(action.payload)
    },
    setSearchList(state, action) {
      state.searchList = action.payload
    }
  },
  extraReducers: (builder) => {
    builder
      .addCase(fetchList.pending, (state, action) => {
        // 处理请求pending状态，比如显示加载指示器  
        // console.log('Fetching list...');
      })
      .addCase(fetchList.fulfilled, (state, action) => {
        state.list = action.payload.homegood;
      })
      .addCase(fetchList.rejected, (state, action) => {
      })
      .addCase(fetchComments.pending, (state, action) => {
      })
      .addCase(fetchComments.fulfilled, (state, action) => {
        state.comments = action.payload.data;
        // console.log(state.comments);

      })
      .addCase(fetchComments.rejected, (state, action) => {
      })
      .addCase(fetchTopic.pending, (state, action) => {
      })
      .addCase(fetchTopic.fulfilled, (state, action) => {
        state.topic = action.payload.data;
        // console.log(action.payload.data)
        // console.log(1111);
        // console.log(state.topic);
      })
      .addCase(fetchTopic.rejected, (state, action) => {
      });
  },
});

const store = configureStore({
  reducer: stateSlice.reducer,
});

export const { goDetail, setAct, setChat, setMyId, setguang, getguang, setLastExpressions, setSearchList } = stateSlice.actions;
export { fetchList, fetchComments, fetchTopic };
export default store;